<?php

namespace app\admin\controller;

use think\facade\View;

class UserGroup extends Common
{
    public $userGroupObj = null;
    public function __construct()
    {
        parent::__construct();
        $this->userGroupObj = new \app\admin\model\UserGroup();
    }

    /**
     * 用户组列表
     */
    public function index()
    {
        if (request()->isPost()) {
            $list = $this->userGroupObj->select()->toArray();
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list, 'rel' => 1];
        }
        return View::fetch();
    }

    /**
     * 添加用户组
     */

    public function add()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $data['group_create_time'] = time();
                $this->userGroupObj->save($data);
                return ['code' => 0, 'msg' => '操作成功!'];
            } else {
                return View::fetch('edit');
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 编缉用户组
     */
    public function edit()
    {
        try {
            if (request()->isPost()) {
                $user_group_id = input('post.user_group_id');
                $data = [
                    'group_name' => input('post.group_name','','trim'),
                    'group_desc' => input('post.group_desc','','trim'),
                ];
                $this->userGroupObj->updateByUserGroupId($user_group_id,$data);
                return ['code' => 0, 'msg' => '操作成功！'];
            } else {
                $where['user_group_id'] = input('param.user_group_id');
                $info = $this->userGroupObj->where($where)->find();
                $this->assign('info', $info);
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 删除会员等级
     * @author admin
     * @date 2019-08-08
     */
    public function doDelete()
    {
        try {
            $user_group_id = input('param.id',0,'intval');
            if (empty($user_group_id)){
                return ['code' => 1, 'msg' => '用户组ID不存在'];
            }
            $this->userGroupObj->where('user_group_id',$user_group_id)->delete();
            return ['code' => 0, 'msg' => '删除成功！'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

}